//
//  WmcTeamMemberAlbumListVM.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/9/15.
//

import Foundation
import RxRelay

class WmcTeamMemberAlbumListVM: WmcBaseVM {
    var teamAblum: WmcMediaTeamAlbumModel?

    let members: BehaviorRelay<[WmcMediaClassifyAlbumMemberModel]> = .init(value: [])

    let error: BehaviorRelay<Error?> = .init(value: nil)

    /// 获取成员列表
    /// - Returns: description
    func getMembers() {
        guard let albumId = teamAblum?.albumId, let busId = teamAblum?.busId, let teamId = teamAblum?.teamId else {
            return
        }
        WmcHudManager.loading()

        WmcMediaTeamAlbumAPI.fetchClassifyAlbumMemberList(albumId: albumId, busId: busId, currentPage: page, pageSize: pageSize, teamId: teamId).subscribe { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.members.accept(list)
            WmcHudManager.dismiss()
        } onError: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            weakSelf.error.accept(error)
        }.disposed(by: rx.disposeBag)
    }
    
    func getMembers(success: ((_ list: [WmcMediaClassifyAlbumMemberModel]) -> Void)?,failure: ((_ error: Error) -> Void)?) -> Void {
        guard let albumId = teamAblum?.albumId, let busId = teamAblum?.busId, let teamId = teamAblum?.teamId else {
            return
        }
        WmcHudManager.loading()

        WmcMediaTeamAlbumAPI.fetchClassifyAlbumMemberList(albumId: albumId, busId: busId, currentPage: page, pageSize: pageSize, teamId: teamId).subscribe { [weak self] list in
            guard let weakSelf = self else { return }
            weakSelf.members.accept(list)
            WmcHudManager.dismiss()
            guard let success = success else { return }
            success(list)
        } onError: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.toast(error)
            WmcHudManager.dismiss()
            weakSelf.error.accept(error)
            guard let failure = failure else { return }
            failure(error)
        }.disposed(by: rx.disposeBag)
    }
}
